//
//  AWRefreshNormalHeader.m
//  AmberWeather
//
//  Created by LeeSongtao on 16/4/13.
//  Copyright © 2016年 Amber Weather. All rights reserved.
//

#import "SPRefreshNormalHeader.h"

@interface SPRefreshNormalHeader ()
{
    __unsafe_unretained UIImageView *_arrowView;
}
@property (weak, nonatomic) UIActivityIndicatorView *loadingView;

@end

@implementation SPRefreshNormalHeader

#pragma mark - 懒加载子控件
- (UIImageView *)arrowView
{
    if (!_arrowView) {
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:[SPRefreshNormalHeader mj_arrowImage]];
        [self addSubview:_arrowView = arrowView];
    }
    return _arrowView;
}

+ (UIImage *)mj_arrowImage
{
    static UIImage *arrowImage = nil;
    if (arrowImage == nil) {
        arrowImage = [[UIImage imageWithContentsOfFile:[[NSBundle mj_refreshBundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return arrowImage;
}

- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorViewStyle];
        loadingView.hidesWhenStopped = YES;
        [self addSubview:_loadingView = loadingView];
    }
    return _loadingView;
}

#pragma mark - 公共方法
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
    _activityIndicatorViewStyle = activityIndicatorViewStyle;
    
    self.loadingView = nil;
    [self setNeedsLayout];
}

#pragma makr - 重写父类的方法
- (void)prepare
{
    [super prepare];
    self.lastUpdatedTimeLabel.hidden = YES;
    //    normalHeader.stateLabel.hidden = YES;
    [self setTitle:NSLocalizedString(@"正在刷新数据...", @"正在刷新数据...") forState:MJRefreshStateRefreshing];
    [self setTitle:NSLocalizedString(@"正在刷新数据...", @"下拉刷新数据...") forState:MJRefreshStateIdle];
    [self setTitle:NSLocalizedString(@"松开立即刷新", @"松开立即刷新") forState:MJRefreshStatePulling];

    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
}

- (void)placeSubviews
{
    [super placeSubviews];
    self.stateLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.stateLabel.textColor = THWhiteAlplaColor(0.6);

    CGFloat stateLabelH = self.mj_h * 0.5;
    self.stateLabel.mj_x = 0;
    self.stateLabel.mj_y = stateLabelH + 5.;
    self.stateLabel.mj_w = self.mj_w;
    self.stateLabel.mj_h = stateLabelH;
      // 箭头
    if (self.arrowView.constraints.count == 0) {
        self.arrowView.mj_size = self.arrowView.image.size;
        self.arrowView.centerX = self.stateLabel.centerX;
        self.arrowView.centerY = self.mj_h * 0.25 + 5.;
    }
    // 圈圈
    if (self.loadingView.constraints.count == 0) {
        self.loadingView.center = self.arrowView.center;
    }
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing) {
            self.arrowView.transform = CGAffineTransformIdentity;
            
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loadingView.alpha = 0.0;
            } completion:^(BOOL finished) {
                // 如果执行完动画发现不是idle状态，就直接返回，进入其他状态
                if (self.state != MJRefreshStateIdle) return;
                
                self.loadingView.alpha = 1.0;
                [self.loadingView stopAnimating];
                self.arrowView.hidden = NO;
            }];
        } else {
            [self.loadingView stopAnimating];
            self.arrowView.hidden = NO;
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowView.transform = CGAffineTransformIdentity;
            }];
        }
    } else if (state == MJRefreshStatePulling) {
        [self.loadingView stopAnimating];
        self.arrowView.hidden = NO;
        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
        }];
    } else if (state == MJRefreshStateRefreshing) {
        self.loadingView.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
        [self.loadingView startAnimating];
        self.arrowView.hidden = YES;
    }
}
@end
